package drds.binlog.binlog_event.event.unimportance;

import drds.binlog.binlog_event.LogByteBlock;
import drds.binlog.binlog_event.LogEvent;
import drds.binlog.binlog_event.event.FormatDescriptionLogEvent;
import drds.binlog.binlog_event.event.LogHeader;
import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;


@Author(name = Authors.LI_YANG)
public class XaPrepareLogEvent extends LogEvent
{

    @Setter
    @Getter
    private boolean onePhase;
    @Setter
    @Getter
    private int formatId;
    @Setter
    @Getter
    private int gtridLength;
    @Setter
    @Getter
    private int bqualLength;
    @Setter
    @Getter
    private byte[] data;

    public XaPrepareLogEvent(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader);

        final int commonHeaderLength = formatDescriptionLogEvent.getCommonHeaderLength();
        final int postHeaderLength = formatDescriptionLogEvent.getPostHeaderLengths()[logHeader.getLogType() - 1];
        //
        int offset = commonHeaderLength + postHeaderLength;
        logByteBlock.setIndexAsOriginIndexAndOffset(offset);

        onePhase = (logByteBlock.get1SignedIntByIndex() == 0x00 ? false : true);
        formatId = logByteBlock.getLittleEndian4SignedIntByIndex();
        gtridLength = logByteBlock.getLittleEndian4SignedIntByIndex();
        bqualLength = logByteBlock.getLittleEndian4SignedIntByIndex();

        int MY_XIDDATASIZE = 128;
        if (MY_XIDDATASIZE >= gtridLength + bqualLength &&//
                //
                gtridLength >= 0 && //
                gtridLength <= 64 && //
                //
                bqualLength >= 0 && //
                bqualLength <= 64)//
        {
            data = logByteBlock.getBytesByIndex(gtridLength + bqualLength);
        } else
        {
            formatId = -1;
            gtridLength = 0;
            bqualLength = 0;
        }
    }


    public byte[] getData()
    {
        return data;
    }

}
